﻿function AzureOnlineServicePropertiesView(context) {

    var dispatcher = $({});

    var spinner = new SpinnerModel();

    var screen = ko.observable('logging');

    var type = ko.observable('');

    var tablesServiceProperties = ko.observable();
    var blobsServiceProperties = ko.observable();
    var queuesServiceProperties = ko.observable();

    var properties = ko.computed(function () {
        switch (type()) {
            case 'tables':
                return tablesServiceProperties();
            case 'blobs':
                return blobsServiceProperties();
            case 'queues':
                return queuesServiceProperties();
            default:
                return null;
        }
    });

    function initForTables() {
        type('tables');
        tablesServiceProperties(new AzureOnlineServiceProperties());

        var api = context.api();
        spinner.enter();
        api.tables.getServiceProperties({
            success: function (res) {
                spinner.leave();
                tablesServiceProperties(new AzureOnlineServiceProperties($(res)));
            },
            error: function () {
                spinner.leave();
            }
        });
    }

    function initForBlobs() {
        type('blobs');
        blobsServiceProperties(new AzureOnlineServiceProperties());

        var api = context.api();
        spinner.enter();
        api.blobs.getServiceProperties({
            success: function (res) {
                spinner.leave();
                blobsServiceProperties(new AzureOnlineServiceProperties($(res)));
            },
            error: function () {
                spinner.leave();
            }
        });
    }

    function initForQueues() {
        type('queues');
        queuesServiceProperties(new AzureOnlineServiceProperties());

        var api = context.api();
        spinner.enter();
        api.queues.getServiceProperties({
            success: function (res) {
                spinner.leave();
                queuesServiceProperties(new AzureOnlineServiceProperties($(res)));
            },
            error: function () {
                spinner.leave();
            }
        });
    }

    function goToLogging() {
        screen('logging');
    }

    function goToMetrics() {
        screen('metrics');
    }

    function submit() {
        switch (type()) {
            case 'tables':
                return submitForTables();
            case 'blobs':
                return submitForBlobs();
            case 'queues':
                return submitForQueues();
            default:
                return null;
        }
    }

    function submitForTables() {
        var info = tablesServiceProperties().getInfo();

        var api = context.api();
        spinner.enter();
        api.tables.setServiceProperties({
            content: info,
            success: function () {
                spinner.leave();
                dispatcher.trigger('success');
            },
            error: function () {
                spinner.leave();
            }
        });
    }

    function submitForBlobs() {
        var info = blobsServiceProperties().getInfo();

        var api = context.api();
        spinner.enter();
        api.blobs.getServiceProperties({
            content: info,
            success: function () {
                spinner.leave();
                dispatcher.trigger('success');
            },
            error: function () {
                spinner.leave();
            }
        });
    }

    function submitForQueues() {
        var info = queuesServiceProperties().getInfo();

        var api = context.api();
        spinner.enter();
        api.queues.setServiceProperties({
            content: info,
            success: function () {
                spinner.leave();
                dispatcher.trigger('success');
            },
            error: function () {
                spinner.leave();
            }
        });
    }
    
    this.initForTables = initForTables;
    this.initForBlobs = initForBlobs;
    this.initForQueues = initForQueues;

    this.screen = screen;
    this.properties = properties;
    this.goToLogging = goToLogging;
    this.goToMetrics = goToMetrics;
    this.submit = submit;

    this.addOnSuccess = function (handler) {
        dispatcher.bind('success', handler);
    };
}


function AzureOnlineServicePropertiesRetentionPolicy() {

    var self = this;
    
    var enabled = ko.observable(false);

    var days = ko.observable(0);
    
    function escape(str) {
        return $('<div/>').text(str).html();
    }
    
    function getInfo() {
        var res = '';
        res += '<RetentionPolicy>';
        res += '<Enabled>' + escape(enabled()) + '</Enabled>';
        if (enabled() == 'true') {
            res += '<Days>' + escape(days()) + '</Days>';
        }
        res += '</RetentionPolicy>';
        return res;
    }
    
    function setInfo($info) {
        enabled($info.find('Enabled').text());
        days($info.find('Days').text());
    }
    
    self.enabled = enabled;
    self.days = days;
    self.getInfo = getInfo;
    self.setInfo = setInfo;
}

function AzureOnlineServiceProperties($initialInfo) {

    var self = this;

    var defaultServiceVersion = ko.observable('');

    var logging = (function () {

        var version = ko.observable('');
        var logDeletes = ko.observable(false);
        var logReads = ko.observable(false);
        var logWrites = ko.observable(false);
        var retentionPolicy = new AzureOnlineServicePropertiesRetentionPolicy();

        function setLoggingInfo($info) {
            version($info.find('Version').text());
            logDeletes($info.find('Delete').text());
            logReads($info.find('Read').text());
            logWrites($info.find('Write').text());
            retentionPolicy.setInfo($info.find('RetentionPolicy'));
        }

        function getLoggingInfo() {
            var res = '';
            res += '<Logging>';
            res += '<Version>' + escape(version()) + '</Version>';
            res += '<Delete>' + escape(logDeletes()) + '</Delete>';
            res += '<Read>' + escape(logReads()) + '</Read>';
            res += '<Write>' + escape(logWrites()) + '</Write>';
            res += retentionPolicy.getInfo();
            res += '</Logging>';
            return res;
        }

        return {
            version: version,
            logDeletes: logDeletes,
            logReads: logReads,
            logWrites: logWrites,
            getInfo: getLoggingInfo,
            setInfo: setLoggingInfo,
            retentionPolicy: retentionPolicy
        };
    })();

    var metrics = (function () {

        var version = ko.observable('');
        var enabled = ko.observable(false);
        var includeApis = ko.observable('');

        var retentionPolicy = new AzureOnlineServicePropertiesRetentionPolicy();

        function setMetricsInfo($info) {
            version($info.find('Version').text());
            enabled($info.children('Enabled').text());
            includeApis($info.find('IncludeAPIs').text());
            retentionPolicy.setInfo($info.find('RetentionPolicy'));
        }

        function getMetricsInfo() {
            var res = '';
            res += '<Metrics>';
            res += '<Version>' + escape(version()) + '</Version>';
            res += '<Enabled>' + escape(enabled()) + '</Enabled>';
            if (enabled() == 'true') {
                res += '<IncludeAPIs>' + escape(includeApis()) + '</IncludeAPIs>';
            }
            res += retentionPolicy.getInfo();
            res += '</Metrics>';
            return res;
        }
        
        return {
            version: version,
            enabled: enabled,
            includeApis: includeApis,
            getInfo: getMetricsInfo,
            setInfo: setMetricsInfo,
            retentionPolicy: retentionPolicy
        };
    })();

    function escape(str) {
        return $('<div/>').text(str).html();
    }

    function getInfo() {
        var res = '';
        res += '<?xml version="1.0" encoding="utf-8"?>';
        res += '<StorageServiceProperties>';
        res += logging.getInfo();
        res += metrics.getInfo();
        res += '</StorageServiceProperties>';
        return res;
    }

    function setInfo($info) {
        logging.setInfo($info.find('Logging'));
        metrics.setInfo($info.find('Metrics'));
        defaultServiceVersion($info.find('DefaultServiceVersion').text());
    }

    if ($initialInfo) {
        setInfo($initialInfo);
    }

    self.getInfo = getInfo;
    self.setInfo = setInfo;
    self.logging = logging;
    self.metrics = metrics;
}
